<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\UserModel;
use cmf\controller\AdminBaseController;
use think\Db;

//专家
class ExpertController extends AdminBaseController
{
    public function index(){
        $param = $this->request->param();
        $recommend = UserModel::EXPERT_RECOMMEND;
        $expert = UserModel::EXPERT_TYPE;
        $list = Db::name('user')
            ->where('is_expert',UserModel::EXPERT_YES)
            ->where(function($query) use($param){
                if(isset($param['user_nickname']) && !empty($param['user_nickname'])){
                    $name = $param['user_nickname'];
                    $query->where('user_nickname','like','%'.$name.'%');
                }
            })
            ->field('*')
            ->order('id desc')
            ->paginate(null,false,['query'=>$param])
            ->each(function($item, $key) use($recommend,$expert){
                $item['is_recommend'] = $recommend[$item['is_recommend']];
                $type = explode(',',$item['type']);
                $item['type'] = [];
                foreach($type as $value){
                    array_push($item['type'],$expert[$value]);
                }
                $item['type'] = implode("，",$item['type']);
                return $item;
            });
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    //添加页面
    public function add(){
        //是否推荐
        $this->assign('recommend',UserModel::EXPERT_RECOMMEND);
        //类型
        $this->assign('type',UserModel::EXPERT_TYPE);
        return $this->fetch();
    }

    //添加提交
    public function addPost(){
        $data = $this->request->param();
        $result = $this->validate($data,'Expert.add');
        if($result !== true){
            $this->error($result);
        }
        $data['user_pass'] = cmf_password($data['user_pass']);
        $data['type'] = implode(',',$data['type']);
        $data['is_expert'] = UserModel::EXPERT_YES;
        $data['create_time'] = time();
        $result = DB::name('user')->insertGetId($data);
        if ($result !== false) {
            Db::name('RoleUser')->insert(["role_id" => UserModel::EXPERT_ROLE, "user_id" => $result]);
            $this->success("添加成功！", url("Expert/index"));
        } else {
            $this->error("添加失败！");
        }
    }

    //编辑页面
    public function edit(){
        $id = $this->request->param('id');
        $post = Db::name('user')
            ->where(['id'=>$id])
            ->find();
        $this->assign('post',$post);
        //是否推荐
        $this->assign('recommend',UserModel::EXPERT_RECOMMEND);
        //类型
        $this->assign('type',UserModel::EXPERT_TYPE);
        return $this->fetch();
    }

    //编辑提交
    public function editPost(){
        $data = $this->request->param();
        $result = $this->validate($data,'Expert.edit');
        if($result !== true){
            $this->error($result);
        }

        if(empty($data['user_pass'])) {
            unset($data['user_pass']);
        } else {
            $data['user_pass'] = cmf_password($data['user_pass']);
        }
        $data['type'] = implode(',',$data['type']);
        $data['is_expert'] = UserModel::EXPERT_YES;
        $result = DB::name('user')->update($data);
        if ($result !== false) {
            $this->success("保存成功！", url("Expert/index"));
        } else {
            $this->error("保存失败！");
        }
    }

    //删除
    public function delete(){
        $id = $this->request->param('id');
        $userModel = new UserModel();
        $res = $userModel->where('id',$id)->delete();
        if($res){
            $this->success('删除成功！');
        }else{
            $this->error('删除失败!');
        }

    }
}